package com.agent.vpsserver.common.utils.stream;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.codec.Base64Encoder;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * description
 *
 * @author <a href="mailto:ludezh@dingtalk.com"> ludezh </a>
 * @version 1.0.0   2020/3/16,11:34
 * @since 1.0.0     2020/3/16,11:34
 */
public class GzipUtils {
    /**
     * 使用gzip进行压缩
     */
    public static String zip(String primStr) {
        if (primStr == null || primStr.length() == 0) {
            return primStr;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        GZIPOutputStream gzip = null;
        try {
            gzip = new GZIPOutputStream(out);
            gzip.write(primStr.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (gzip != null) {
                try {
                    gzip.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        return Base64Encoder.encode(out.toByteArray());
    }

    /**
     * <p>Description:使用gzip进行解压缩</p>
     *
     * @param compressedStr
     * @return
     */
    public static String unzip(String compressedStr) {
        if (compressedStr == null) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ByteArrayInputStream in = null;
        GZIPInputStream ginzip = null;
        byte[] compressed = null;
        String decompressed = null;
        try {
            compressed = Base64Decoder.decode(compressedStr);
            in = new ByteArrayInputStream(compressed);
            ginzip = new GZIPInputStream(in);

            byte[] buffer = new byte[1024];
            int offset = -1;
            while ((offset = ginzip.read(buffer)) != -1) {
                out.write(buffer, 0, offset);
            }
            decompressed = out.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (ginzip != null) {
                try {
                    ginzip.close();
                } catch (IOException e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }

        return decompressed;
    }

//    public static void main(String[] args) {
//        String zip = zip("http://rd.wechat.com/qrcode/confirm?block_type=101&content=%E3%80%90%E6%B7%98%E5%AE%9D%E4%BB%B7%E3%80%91+++%C2%A5138.0%0A%E3%80%90%E5%88%B8%E5%90%8E%E4%BB%B7%E3%80%91+++%C2%A5133.0%0A%E3%80%90%E9%A2%86%E5%88%B8%E4%B8%8B%E5%8D%95%E7%9C%81%E3%80%91++%C2%A55%0A----------------%0A%E5%BE%A9%E8%A3%BD%E8%AF%84%E8%AE%BA%E2%82%B3b9NT1hAYW95%E2%82%B3%2C%E5%8E%BB%E3%80%90tao%E5%AF%B3%E3%80%91%E4%B8%8B%E5%8D%95&lang=zh_CN&scene=34&pass_ticket=GLXVTj6nQm6mtuKmvL%2BvFpF1nBQa11OP96U432sJ5q72VIRdeZ4BUG%2FUGPJxMW4T");
//        System.out.printf(zip);
//    }
}
